<?php
	session_start();

	require_once("includes/connect.php");
	require_once("includes/function.php");

	$panngan2 = $_REQUEST["panngan2"];
	$panngan3 = $_REQUEST["panngan3"];

	$month_year = $_REQUEST["month_year"];

	$budget_year = $_REQUEST["budget_year"];

	$ex1_id = $_REQUEST["ex1_id"];
	$ex2_id = $_REQUEST["ex2_id"];
	$ex3_id = $_REQUEST["ex3_id"];

	$val = $_POST["val"];

	$array1 = $_POST["array1"];

	$pet_id = $_REQUEST["pet_id"];

	$task = $_REQUEST["task"];

	switch($task) {
		case "new":
			$db = NEW myDBs();
			$running = $db->get_running($budget_year);

			for($i=0; $i < count($val); $i++) {
				$str_in = $str_in. "'". $val[$i]. "',";
			}
			$str_in = $str_in . "''";
			
			$sql = "SELECT * FROM tbl_init
						WHERE  card_id IN ($str_in)
						AND panngan2_id = '$panngan2'
						AND panngan3_id = '$panngan3'
						AND month_year = '$month_year'
						AND budget_year = '$budget_year'
					";
			
			//echo $sql;

			
			//$db1->selectDB("account");
			$result = $db->runSQL($sql);
			
			$db1 = NEW myDBS;
			$db1->selectDB("account");

			$db2 = NEW myDBS;
			$db2->selectDB("account");
                        
                                          
			while ( $rows = $db->fetch_object ( $result )){

                                            switch ($ex3_id) {
                                               case "8":
                                                           $sql= "select (tax+cremation1+welfare+cooperative+loan+bongkum+ghb+aia+cremation2+other2) as total_all FROM tbl_init where card_id = '$rows->card_id' and month_year = '$month_year'";
				
				$result2= $db2->runSQL($sql);
				$rows2 = $db2->fetch_object ( $result2 );
				
				//echo $rows2->total_all;
                                

				$sql = "INSERT INTO tbl_salary 
				(id, lvl1_id, panngan2_id, panngan3_id, ex1_id, ex2_id, ex3_id, month_year, budget_year, salary, pet_id, exp_total)
				VALUES('$rows->card_id', '".$_SESSION["lvl1_id"]."', '$panngan2', '$panngan3', '$ex1_id', '$ex2_id', '$ex3_id', '$month_year', '$budget_year', '$rows->salary', '$running', '$rows2->total_all')
				";
				//echo $sql;

				$db1->runSQL($sql);

                                                   break;
                                               case "9":
                                                   $sql = "INSERT INTO tbl_salary 
				(id, lvl1_id, panngan2_id, panngan3_id, ex1_id, ex2_id, ex3_id, month_year, budget_year, salary, pet_id)
				VALUES('$rows->card_id', '".$_SESSION["lvl1_id"]."', '$panngan2', '$panngan3', '$ex1_id', '$ex2_id', '$ex3_id', '$month_year', '$budget_year', '$rows->extra', '$running')
				";
				//echo $sql;

				$db1->runSQL($sql);
                                                   
                                                   break;
                                               case "10":
                                                   
                                                   $sql = "INSERT INTO tbl_salary 
				(id, lvl1_id, panngan2_id, panngan3_id, ex1_id, ex2_id, ex3_id, month_year, budget_year, salary, pet_id)
				VALUES('$rows->card_id', '".$_SESSION["lvl1_id"]."', '$panngan2', '$panngan3', '$ex1_id', '$ex2_id', '$ex3_id', '$month_year', '$budget_year', '$rows->emoluments', '$running')
				";
				//echo $sql;

				$db1->runSQL($sql);
                                                   break;
                                               default :
                                                   
                                                   break;
                                        }
				

			}
                                     switch ($ex3_id) {
                                               case "8":
                                                   $status = ",salary";
                                                   break;
                                               case "9":
                                                   $status = ",extra";
                                                   
                                                   break;
                                               case "10":
                                                   $status = ",emoluments";
                                                   //$amount1 = $rows->emoluments;
                                                   break;
                                               default :
                                                   $status = "";
                                                   //$amount1 = -1;
                                                   break;
                                        }
			$sql = "UPDATE tbl_init SET status = concat(status,'$status')
				WHERE  card_id IN ($str_in)
				AND panngan2_id = '$panngan2'
				AND panngan3_id = '$panngan3'
				AND month_year = '$month_year'
				AND budget_year = '$budget_year'
				";
			$db1->runSQL($sql);
			//echo $running;

			break;
	case "edit":
			$db = NEW myDBS();
			$db->selectDB("account");
			$sql = "UPDATE tbl_salary 
					SET panngan2_id = '$panngan2',
					panngan3_id = '$panngan3',
					ex1_id = '$ex1_id',
					ex2_id = '$ex2_id',
					ex3_id = '$ex3_id',
					month_year = '$month_year',
					budget_year = '$budget_year'
					WHERE pet_id = '$pet_id'
					";
			//echo $array1[1];
			//echo $sql;
			$db->runSQL($sql);


			$sql = "select id from tbl_salary where pet_id = '$pet_id' order by id";
			$result = $db->runSQL($sql);

			$db1 = NEW myDBS;
			$db1->selectDB("account");
			$i=0;
			while ($rows = $db->fetch_object($result)) {
				$sql = "UPDATE tbl_salary SET salary = '". $array1[$i] ."'
						WHERE id = '". $rows->id ."' and pet_id = '$pet_id'";
				$db1->runSQL($sql);
				//echo $sql;
				$i++;
				/*
				$sql = "UPDATE tbl_init SET "
						
				switch ($ex3_id) {
					case "23":
						$sql = $sql ."rent_home = '". $array1[$i] ."'";
						break;
					case "24":
						$sql = $sql ."education = '". $array1[$i] ."'";
						break;
					case "25":
						$sql = $sql ."medical_fee = '". $array1[$i] ."'";
						break;
				}

				$sql = $sql. " WHERE id = '". $rows->id ."' and month_year = '$month_year'";
				$db1->runSQL($sql);
				$i++;
				*/
			}

			break;
	case "del":
		$db = NEW myDBS();

		$sql = "DELETE FROM tbl_salary WHERE pet_id = '$pet_id'";
		$db->runSQL($sql);
		//echo $sql;
		break;
	}
?>
